
package com.ifcar.app.view;

import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.PopupWindow;

/**
 * 自定义PopupWindow.<br/>
 * 可以显示后可以点击回退消失，触摸外部时消失
 * 
 * @author wjying
 */
public class MyPopupWindow extends PopupWindow {

    private Context mContext;

    private ContentFrame mContent;

    private OnTouchListener mTouchInterceptor;

    public MyPopupWindow(Context context, int width, int height) {
        super(context);
        mContext = context;
        mContent = new ContentFrame(context);
        setContentView(mContent);

        setWidth(width);
        setHeight(height);
        setFocusable(true);
        setBackgroundDrawable(null);
        update();
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        final int keyCode = event.getKeyCode();
        final int action = event.getAction();

        if (isShowing() && checkDismissKey(keyCode)) {
            if (action == KeyEvent.ACTION_UP) {
                dismiss();
            }
            return true;
        }
        return false;
    }

    protected boolean checkDismissKey(int keyCode) {
        return keyCode == KeyEvent.KEYCODE_BACK;
    }

    @Override
    public void setTouchInterceptor(OnTouchListener l) {
        mTouchInterceptor = l;
    }

    @Override
    public View getContentView() {
        return mContent.getChildAt(0);
    }

    public void setContentView(int layout) {
        View contentView = null;
        if (layout != 0) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            contentView = inflater.inflate(layout, null);
        }
        setContentViewInternal(contentView);
    }

    @Override
    public void setContentView(View contentView) {
        setContentViewInternal(contentView);
    }

    private void setContentViewInternal(View contentView) {
        if (mContent == contentView) {
            super.setContentView(contentView);
        } else {
            mContent.removeAllViews();
            if (contentView != null) {
                mContent.addView(contentView);
            }
            update();
        }
    }
    
    protected void onOutSideTouched(MotionEvent event) {
        dismiss();
    }

    private class ContentFrame extends FrameLayout {

        public ContentFrame(Context context) {
            super(context);
        }

        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if (MyPopupWindow.this.dispatchKeyEvent(event)) {
                return true;
            }
            return super.dispatchKeyEvent(event);
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            if (mTouchInterceptor != null && mTouchInterceptor.onTouch(this, ev)) {
                return true;
            }
            return super.dispatchTouchEvent(ev);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            final int x = (int) event.getX();
            final int y = (int) event.getY();

            if ((event.getAction() == MotionEvent.ACTION_DOWN)
                    && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) {
                onOutSideTouched(event);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                onOutSideTouched(event);
                return true;
            } else {
                return super.onTouchEvent(event);
            }
        }

    }
}
